package com.zygh.lz.controller.app;

import com.alibaba.fastjson.JSONObject;
import com.zygh.lz.dao.SectionMapper;
import com.zygh.lz.entity.InformationUser;
import com.zygh.lz.entity.Message;
import com.zygh.lz.entity.Section;
import com.zygh.lz.entity.app.AppNote;
import com.zygh.lz.entity.app.AppTrailerInfo;
import com.zygh.lz.service.InformationService;
import com.zygh.lz.service.MessageService;
import com.zygh.lz.service.impl.AppTrailerInfoService;
import com.zygh.lz.service.impl.InformationUserService;
import com.zygh.lz.stapprove.*;
import com.zygh.lz.vo.ResultBean;
import com.zygh.lz.vo.ResultBeans;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@Api(tags = "App消息通知类", description = "AppMessageController")
@RestController
public class AppMessageController {

    private final InformationService informationService;

    @Autowired
    private MessageService messageService;
    @Autowired
    private InformationUserService informationUserService;
    @Resource
    private AppTrailerInfoService appTrailerInfoService;



    @Autowired
    public AppMessageController(InformationService informationService) {
        this.informationService = informationService;
    }


    @PostMapping("slectAllmessage")
    @ViLog(logType = "1", module = "消息列表>查询所有消息列表")
    public OutBean slectAllmessage(@RequestBody(required = false) InBean inBean) {
        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
        String acceptWarningSignal = jsonObject.getString("acceptWarningSignal");
        String isRead = jsonObject.getString("isRead");
        ResultBeans resultBean = informationService.findInformationByType(acceptWarningSignal, isRead);
        return OutBeanUtil.resSuc(resultBean, inBean);
    }

    @ApiOperation("APP修改消息状态")
    @PostMapping("updaMessage")
    @ViLog(logType = "3", module = "消息列表>修改消息")
    public OutBean updaMessage(InformationUser informationUser, @RequestBody(required = false) InBean inBean) {
        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
        informationUser.setId(Integer.parseInt(jsonObject.getString("id")));
        informationUser.setIsRead(Boolean.valueOf(jsonObject.getString("isRead")));
        ResultBeans resultBeans = informationUserService.updateIgnoreNull(informationUser);
        return OutBeanUtil.resSuc(resultBeans, inBean);
    }

    @ApiOperation("APP未读消息数量")
    @PostMapping("finalReading")
    @ViLog(logType = "1", module = "消息列表>APP未读消息数量")
    public OutBean finalReading(@RequestBody(required = false) InBean inBean) {
        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
        String acceptWarningSignal = jsonObject.getString("acceptWarningSignal");
        ResultBeans resultBeans = informationUserService.finalReading(acceptWarningSignal);
        return OutBeanUtil.resSuc(resultBeans, inBean);
    }


    @ApiOperation("PC删除消息状态")
    @GetMapping("delMessages")
    @ViLog(logType = "4", module = "消息列表>删除消息")
    public ResultBean delMessages(Integer id) {
        return messageService.delMessage(id);
    }

    @ApiOperation("APP删除消息状态")
    @PostMapping("delMessage")
    @ViLog(logType = "4", module = "消息列表>删除消息")
    public OutBean delMessage(@RequestBody(required = false) InBean inBean) {
        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
        AppTrailerInfo appTrailerInfo = new AppTrailerInfo();
        appTrailerInfo.setIsDel(1);
        appTrailerInfo.setSignboard(0);
        appTrailerInfo.setId(Integer.valueOf(jsonObject.getString("id")));
        ResultBeans resultBean = ResultUtil.resultBeansSuccess(appTrailerInfoService.updateById(appTrailerInfo));
        return OutBeanUtil.resSuc(resultBean, inBean);
    }


    @ApiOperation("PC修改消息状态")
    @GetMapping("updaMessages")
    public ResultBean updaMessages(Message message) {

        return messageService.updaMessage(message);
    }


    @GetMapping("findAllmessage")
    @ViLog(logType = "1", module = "消息列表>查询所有消息列表")
    public ResultBean findAllmessage(Integer messageState, Integer accpetId) {

        return messageService.findAllmessage(messageState, accpetId);
    }


}
